#include <QTimer>
#include <QDebug>
#include <QByteArray>
#include <QSystemSemaphore>
#include "SingleApplication.h"


SingleApplication::SingleApplication(int &argc, char *argv[], const QString uniqueKey) : QApplication(argc, argv)
{
    sharedMemory.setKey(uniqueKey);

    if (sharedMemory.isAttached()){
        _isRunning = true;
        log();
    }else {
        _isRunning = false;
        if (!sharedMemory.create(4)) {
            qDebug("不能创建共享内存区!");
            qDebug()<<sharedMemory.errorString();
            return;
        }
        sharedMemory.attach();
    }
}
void SingleApplication::log()
{
    qDebug("已经有程序在运行中，本次程序即将停止!");
}


// public functions.
bool SingleApplication::isRunning()
{
    return _isRunning;
}

void SingleApplication::exit()
{
    sharedMemory.detach();
    qApp->exit(0);
}




